# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time

#from jms.dm import dm__dm_network_end_dispatch_diff_detail
from utils.alerts.doris_telnet_check import net_is_used
from utils.operators.rich_sql_sensor import RichSqlSensor

label = f"dws_network_end_dispatch_diff_rate{int(time())}"
timeout = timedelta(minutes=60).seconds
dorisConn = net_is_used()
doris_jms_dws__dws_network_end_dispatch_diff_rate = RichSqlSensor(
    task_id='doris_jms_dws__dws_network_end_dispatch_diff_rate',
    email=['guoruiling@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool='broker_load_pool',
    conn_id='doris_uat',
    pre_sql=f"""
             TRUNCATE TABLE jms_dws.dws_network_end_dispatch_diff_rate PARTITION (p{{{{ execution_date | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-3) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-4) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-5) | cst_ds_nodash }}}},p{{{{ execution_date | date_add(-6) | cst_ds_nodash }}}});
             LOAD LABEL jms_dws.{label} (
                 DATA INFILE(
				 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | date_add(-6) | cst_ds }}}}/*",
				 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | date_add(-5) | cst_ds }}}}/*",
				 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | date_add(-4) | cst_ds }}}}/*",
				 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | date_add(-3) | cst_ds }}}}/*",
				 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
				 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
                 "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_dm.db/external/dm_network_end_dispatch_rate_dt/dt={{{{ execution_date | cst_ds }}}}/*")
                 INTO TABLE dws_network_end_dispatch_diff_rate
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers_uat | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dws WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dws WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

#doris_jms_dws__dws_network_end_dispatch_diff_rate << dm__dm_network_end_dispatch_diff_detail
